package com.tt.aile.item.service;

import com.tt.aile.item.mapper.CategoryMapper;
import com.tt.aile.item.pojo.Category;
import com.tt.aile.item.pojo.Spu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**根据父类目id查询所有子节点
 * @author 86176 shkstrat
 * @date 2020/9/20 - 20:55
 */
@Service
public class CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    //得到分类名称
    public List<Category> queryCategoriesByPid(Long pid) {

        Category category = new Category();
        category.setParentId(pid);
        return this.categoryMapper.select(category);
    }

    //得到cid的名称，返回
    public List<String> queryNamesByIds(List<Long> ids) {

        List<Category> categories = this.categoryMapper.selectByIdList(ids);
//        System.out.println(categories.size()+"CategoryService-------------ssssssssssssssssssssssssssssss");
        //Stream流直接到的分类得名称并转换成list集合返回
        return categories.stream().map(category -> category.getName()).collect(Collectors.toList());
    }

    public String queryNameById(Long id) {

        Category category = this.categoryMapper.selectByPrimaryKey(id);
        if (category==null){
            return null;
        }
//        System.out.println(category.getName());
        return category.getName();
    }
}
